package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Task;
import util.DBHelp;
import util.RowMapper;

public class TaskDao {

	private DBHelp<Task> db=new DBHelp<Task>();
	
	public void save(Task task){
		String sql="INSERT INTO t_task(id,t_name,start_time,end_time,description,state,`level`,rate,createtime,user_id,goal_id) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql,task.getId(),task.getName(),task.getStart_time(),task.getEnd_time(),task.getDescripion(),task.getState(),task.getLevel(),task.getRate(),task.getCreatetime(),task.getUser_id(),task.getGoal_id());
	}

	public List<Task> findAllByTask() {
		String sql="select id,t_name,start_time,end_time,description,state,`level`,rate,createtime,user_id,goal_id from t_task order by createtime desc";
		return db.executeQueryToList(sql,new TaskRowMapper());
		
		
	}
	
	public class TaskRowMapper implements RowMapper<Task>{

		public Task mapRow(ResultSet rs) throws SQLException {
			Task t=new Task();
			t.setId(rs.getString("id"));
			t.setCreatetime(rs.getString("createtime"));
			t.setDescripion(rs.getString("description"));
			t.setEnd_time(rs.getString("end_time"));
			t.setGoal_id(rs.getString("goal_id"));
			t.setLevel(rs.getString("level"));
			t.setName(rs.getString("t_name"));
			t.setRate(rs.getString("rate"));
			t.setStart_time(rs.getString("start_time"));
			t.setState(rs.getString("state"));
			t.setUser_id(rs.getString("user_id"));
			
			return t;
		}
		
		
	}
		
	
}
